package org.vectrics.recruiter.struts.admin;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionForm;
import org.vectrics.SystemException;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobStatus;
import org.vectrics.recruiter.job.JobStatusReasonType;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;


public class SaveJobStatusReasonAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(SaveJobStatusReasonAction.class);
	private Person person = null;
	
    public SaveJobStatusReasonAction() {
    	super();
    }


	public String doExecute(VectricsActionData actionData)	throws JspException {
		ActionForm form = actionData.getForm();
		Request request = actionData.getRequest();
		
		JobStatusReasonForm jobForm = (JobStatusReasonForm)form;
		String forwardCode = "success";
		
		
		if (actionData.wasButtonPressed("cancel") == false) {
			try {
				person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(ThreadProperties.getDomain(), request);
			}
			catch (Exception ex) {
				log.error("Exception finding auth user", ex);
				throw new JspException("Exception finding auth user", ex);
			}
	
	        log.debug("execute() called, reading http parameters");
	        Job job = null;
	        
			if ((jobForm.getJobId() == null) || (jobForm.getJobId().trim().equals(""))) {
				throw new SystemException("Job id not specified in action form");
			}
			else {
				log.debug("execute() - attempting to find job.");
			    Long jobId = new Long(jobForm.getJobId());
				job = RecruitServiceLocator.getJobProvider().findJob(jobId);
	            
				if (person == null) {
					throw new JspException("Could not find user logged in.");
				}
			}

			JobStatusReasonType reasonType = RecruitServiceLocator
				.getJobProvider().findJobStatusReasonType(jobForm.getJobStatusReasonTypeCode(), job.getDomain());
			job.getCurrentStatus().setStatusReasonType(reasonType);
			job.getCurrentStatus().setDescription(jobForm.getDescription());
			if (job.getCurrentStatus().getStatusType().getCode().equals(JobStatusType.STATUS_POSTED_PENDING)) {
				try {
					job.getCurrentStatus().setPendingPostDate(
							CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(actionData.getLocale()).parse(jobForm.getPendingPostedDate()));
				} catch (Exception ex) {
					log.warn("Bad date format: " + jobForm.getPendingPostedDate());
				}
			}
			RecruitServiceLocator.getJobProvider().updateJob(job);
			forwardCode = "success";
		} else {
		    log.debug("Cancel button pressed.");
		    forwardCode = "cancel";
			if ((jobForm.getJobId() == null) || (jobForm.getJobId().trim().equals(""))) {
				forwardCode = "cancel.new";
			}
		}
    	ThreadProperties.forcedCommitAndNewTransaction();
		ThreadProperties.getSession().getSessionFactory().evict(JobStatus.class);
		
		return (forwardCode);
    }
    
}
